﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Symbonix.NorSoNet.Communication;
using Symbonix.NorSoNet.Communication.DAL.Repositories;
using Symbonix.NorSoNet.Security.BLL.Membership;

/// <summary>
/// Summary description for SiteUtility
/// </summary>
public class SiteUtility
{
    public SiteUtility(){}
    public int CurrentLoggedInUserId
    {
        get
        {
            if (System.Web.HttpContext.Current.Session["_CURRENT_LOGGEDIN_USER_ID"] != null)
            {
                return (int)System.Web.HttpContext.Current.Session["_CURRENT_LOGGEDIN_USER_ID"];
            }
            else
            {
                SystemMembershipFactory membershipFactory = new SystemMembershipFactory();
                IUser member = membershipFactory.GetCurrentUser();
                System.Web.HttpContext.Current.Session["_CURRENT_LOGGEDIN_USER_ID"] = member.Id;
                return member.Id;
            }
        }
       

    }

    public IList<FriendData> MyFriends
    {
        get
        {
            if (System.Web.HttpContext.Current.Session["_CURRENT_LOGGEDIN_USER_FRIENDS"] != null)
            {
                return (IList<FriendData>)System.Web.HttpContext.Current.Session["_CURRENT_LOGGEDIN_USER_FRIENDS"];
            }
            else
            {
                IList<FriendData> friends=LoadFriendsData();
                System.Web.HttpContext.Current.Session["_CURRENT_LOGGEDIN_USER_FRIENDS"]=friends;
                return friends;
            }
        }
        private set
        {
            System.Web.HttpContext.Current.Session["_CURRENT_LOGGEDIN_USER_FRIENDS"] = value;
        }

    }
    private IList<FriendData> LoadFriendsData()
    {
        SystemMembershipFactory membershipFactory = new SystemMembershipFactory();
        IUser member = membershipFactory.GetCurrentUser();
        FriendDataRepository repository = new FriendDataRepository();
        return repository.GetAllFriends(member.Id);

    }
    public bool ResetCurrentLoggegInUsersFriendList()
    {
        try
        {
            FriendDataRepository repository = new FriendDataRepository();
            MyFriends = repository.GetAllFriends(CurrentLoggedInUserId);
            return true;
        }
        catch
        {
            return false;
        }
    }
}